﻿namespace Hidistro.UI.SaleSystem.CodeBehind
{
    using ASPNET.WebControls;
    using Hidistro.Core.Entities;
    using Hidistro.Core.Enums;
    using Hidistro.Entities.Commodities;
    using Hidistro.Membership.Context;
    using Hidistro.SaleSystem.Catalog;
    using Hidistro.UI.Common.Controls;
    using System;
    using System.IO;
    using System.Web.UI.WebControls;

    public class SubPage : HtmlTemplatedWebControl
    {
        private int categoryId;
        private Literal liimg;
        public SubPage()
        {

            int.TryParse(this.Page.Request.QueryString["CategoryId"], out this.categoryId);
            CategoryInfo category = CategoryBrowser.GetCategory(this.categoryId);
            if (((category != null) && (category.Depth == 1)) && !(string.IsNullOrEmpty(category.Theme) || !File.Exists(HiContext.Current.Context.Request.MapPath(HiContext.Current.GetSkinPath() + "/categorythemes/" + category.Theme))))
            {
                this.SkinName = "/categorythemes/" + category.Theme;
            }
            this.LoadPageSearch(category);
        }
        private void LoadPageSearch(CategoryInfo category)
        {
            if (!string.IsNullOrEmpty(category.MetaKeywords))
            {
                MetaTags.AddMetaKeywords(category.MetaKeywords, HiContext.Current.Context);
            }
            if (!string.IsNullOrEmpty(category.MetaDescription))
            {
                MetaTags.AddMetaDescription(category.MetaDescription, HiContext.Current.Context);
            }
            if (!string.IsNullOrEmpty(category.MetaTitle))
            {
                PageTitle.AddSiteNameTitle(category.MetaTitle);
            }
            else
            {
                PageTitle.AddSiteNameTitle(category.Name);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                if (this.categoryId == 9)
                {
                    this.SkinName = "Skin-SubPage-9.html";
                }
                else
                {
                    this.SkinName = "Skin-SubPage.html";
                }
            }
            base.OnInit(e);
        }



        //private Pager pager;
        private ThemedTemplatedRepeater rptProduct;
        protected override void AttachChildControls()
        {
            this.liimg = (Literal)this.FindControl("liimg");
            this.rptProduct = (ThemedTemplatedRepeater)this.FindControl("rptProduct");
          //  this.pager = (Pager)this.FindControl("pager");
            if (!this.Page.IsPostBack)
            {
                this.BindProduct();
            }
        }

        private void BindProduct()
        {
            DbQueryResult countDownProductList = ProductBrowser.GetCountDownProductList(this.GetProductBrowseQuery());
            this.rptProduct.DataSource = countDownProductList.Data;
            this.rptProduct.DataBind();
            string imgNamber = this.categoryId.ToString();
            this.liimg.Text = "<img src=\"http://image.juseshop.com/images/category/" + imgNamber + ".jpg\" width=\"740\" height=\"320\">";

            //this.pager.TotalRecords = countDownProductList.TotalRecords;
        }

        private ProductBrowseQuery GetProductBrowseQuery()
        {
            return new ProductBrowseQuery { IsCount = true, PageIndex = 1, PageSize = 20, SortBy = "DisplaySequence", SortOrder = SortAction.Desc };
        }
    }
}

